package com.wqtz.chechai.view.activities;

import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.os.Handler;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.avos.avoscloud.AVUser;
import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.FirstPagerAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.wqtz.chechai.widget.GestureView.STATE_LOGIN;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2016/12/19 9:50
 */

public class ShowFirstActivity extends BaseActivity {
	@BindView(R.id.vp_logo)
	ViewPager mVpLogo;
	@BindView(R.id.ll)
	LinearLayout mLl;

	List<ImageView> imagelist ;
	private  int images[];
	FirstPagerAdapter ad ;
	private int currentItem;
	private final int UNLOCK = 10086;


	@Override
	public void init() {
		new Handler().postDelayed(new Runnable()
		{
			public void run()
			{
				if(AVUser.getCurrentUser()!=null){
					startActivityForResult(new Intent(ShowFirstActivity.this,SignUserGestureActivity.class),UNLOCK);
					startActivity(new Intent(ShowFirstActivity.this, MainActivity.class));
					ShowFirstActivity.this.finish();
				}else {
					startActivity(new Intent(ShowFirstActivity.this, LoginActivity.class));
					ShowFirstActivity.this.finish();
				}
			}
		},2000);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
		ButterKnife.bind(this);
		images = new int[]{R.drawable.logo_1,R.drawable.logo_2};
		imagelist = new ArrayList<>();
		for (int i = 0; i < images.length; i++) {
			ImageView iv = new ImageView(this);
			iv.setImageResource(images[i]);
			iv.setScaleType(ImageView.ScaleType.FIT_XY);
			imagelist.add(iv);
		}
		ad = new FirstPagerAdapter(imagelist);
		mVpLogo.setAdapter(ad);
		mVpLogo.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {


			}

			@Override
			public void onPageSelected(int position) {
				currentItem = position;
				Logger.e(position+"onPageSelected");

			}
			@Override
			public void onPageScrollStateChanged(int state) {
				Logger.e(state+"onPageScrollStateChanged");
			}
		});

		mVpLogo.setOnTouchListener(new View.OnTouchListener() {
			float startX;
			float startY;//没有用到
			float endX;
			float endY;//没有用到
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()){
					case MotionEvent.ACTION_DOWN:
						startX=event.getX();
						startY=event.getY();
						break;
					case MotionEvent.ACTION_UP:
						endX=event.getX();
						endY=event.getY();
						WindowManager windowManager= (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);

						//获取屏幕的宽度
						Point size = new Point();
						windowManager.getDefaultDisplay().getSize(size);
						int width=size.x;

						//首先要确定的是，是否到了最后一页，然后判断是否向左滑动，并且滑动距离是否符合，我这里的判断距离是屏幕宽度的4分之一（这里可以适当控制）
						if(currentItem==(imagelist.size()-1)&&startX-endX>=(width/4)){
							Logger.d("进入触摸模式");
							overridePendingTransition(R.anim.slide_in_right,R.anim.slide_in_left);//这部分代码是切换Activity时的动画，看起来就不会很生硬
							if(AVUser.getCurrentUser()!=null){
								startActivity(new Intent(ShowFirstActivity.this, MainActivity.class));
								ShowFirstActivity.this.finish();
							}else {
								startActivity(new Intent(ShowFirstActivity.this, LoginActivity.class));
								ShowFirstActivity.this.finish();
							}

						}
						break;
				}
				return false;
			}
		});
	}



	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_showfirst;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}



	@OnClick({R.id.vp_logo, R.id.ll})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.vp_logo:
				break;
			case R.id.ll:
				break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		activitys.add(this); //添加当前的activity

	}
}
